// CadToolLine.cpp
//
#include "CadToolLine.h"

#include "CadEngine.h"

#include "OpenGL/Box.h"
#include "OpenGL/OpenGL.h"
#include "OpenGL/Renderer.h"

//-------------------------------------------------------------------------------------------------
CadToolLine::CadToolLine()
: CadTool("Line",2)
{
	_creationColor = OpenGL::Color::red();
}

//-------------------------------------------------------------------------------------------------
CadToolLine::~CadToolLine()
{}

//-------------------------------------------------------------------------------------------------
void CadToolLine::execute()
{
	if( OpenGL::Math::distance(_points[0],_points[1]) < _manager->pixelSize() )
		return;

	CadEngine::Command c;

	c.command   = CadEngine::Create;
	c.primitive = CadEngine::Line;
	c.color     = _creationColor;
	c.points.push_back( _points[0] );
	c.points.push_back( _points[1] );
	c.attributes += _objectAttributes;

	_manager->issueCommand(c);
}

// --------------------------------------------------------------------------------------------
void CadToolLine::setCreationColor( const OpenGL::Color &c ) { _creationColor = c; }

// --------------------------------------------------------------------------------------------
OpenGL::Color CadToolLine::creationColor() const { return _creationColor; }

//-------------------------------------------------------------------------------------------------
void CadToolLine::draw( OpenGL::Renderer *r )
{
	if(_nPoints < 2) return;

	OpenGL::Math::dvec3 p0( _points[0].x,_points[0].y,0 );//-r->origin();
	OpenGL::Math::dvec3 p1( _points[1].x,_points[1].y,0 );//-r->origin();

	glPushAttrib( GL_LINE_BIT|GL_ENABLE_BIT );
	glEnable(GL_LINE_STIPPLE);
	glLineStipple(1, 0x00FF);
	glLineWidth(2);	
	glColor4fv( _manager->settings().previewColor().rgba() );

	glBegin( GL_LINES );
		glVertex2dv( OpenGL::Math::value_ptr(p0-r->origin()) );
		glVertex2dv( OpenGL::Math::value_ptr(p1-r->origin()) );
	glEnd();

	glPopAttrib();
}